//
//  LiveListViewController.m
//  CYWFrame
//
//  Created by Macx on 2017/8/15.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "LiveListViewController.h"
#import "LiveContentViewController.h"
#import "CCZSegementController2.h"
#import "HomeRankViewController.h"
#import "SearchViewController.h"
#import "MessageViewController.h"

@interface LiveListViewController ()

@property (nonatomic, strong) NSMutableArray *tagArr;

@end

@implementation LiveListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self GETliveTags];
    
    [self buildUI];
    
}

#pragma mark ==========构建UI==========
- (void)buildUI {
    
    self.view.backgroundColor = kWhiteColor;

    // 导航栏中间设为图片
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 5.0f, 100.0f, 30.0f)];//初始化图片视图控件
//    imageView.contentMode = UIViewContentModeScaleAspectFit;//设置内容样式,通过保持长宽比缩放内容适应视图的大小,任何剩余的区域的视图的界限是透明的。
//    UIImage *image = [UIImage imageNamed:@"zhibo_rank"];//初始化图像视图
//    [imageView setImage:image];
//    self.navigationItem.titleView = imageView;//设置导航栏的titleView为imageView
//    [imageView addTarget:self action:@selector(clickRank:)];
    
    self.navigationItem.title = @"直播";
    
    // 导航栏左右按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zhibo_search"] style:UIBarButtonItemStylePlain target:self action:@selector(navLeftAction:)];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStylePlain target:self action:@selector(navRithAction:)];
//    [self.navigationItem.rightBarButtonItem setImage:[[UIImage imageNamed:@"zhibo_notice"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
  
}
- (void)createVC {
    NSMutableArray *nameArr = [[NSMutableArray alloc] init];
    NSMutableArray *vcArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < self.tagArr.count; i++) {
        LiveContentViewController *liveVC = [[LiveContentViewController alloc] init];
        liveVC.tagId = [self.tagArr[i] valueForKey:@"id"];
        [nameArr addObject:[self.tagArr[i] valueForKey:@"name"]];
        [vcArr addObject:liveVC];
    }
    
    CCZSegementController2 *segement = [CCZSegementController2 segementControllerWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49-64) titles:nameArr];
    [segement setSegementViewControllers:vcArr];
    
    segement.style = CCZSegementStyleDefault;
    [segement setSegementTintColor:UIMainColorRGB];
    [self.view addSubview:segement.view];
    [self addChildViewController:segement];
    [segement selectedNavAtIndex:^(NSInteger index) {
        NSLog(@"index %ld",index);
    }];
    [segement setSelectedItemAtIndex:self.defaultIndex];
    
    
    
}

#pragma mark ===========响应方法==========
- (void)GETliveTags {
    
    [[NetworkTool sharedTool] requestWithName:@"获取直播标签" URLString:APITags parameters:nil method:@"GET" callBack:^(id responseObject) {
        
        for (NSDictionary *dict in responseObject[@"data"]) {
            [self.tagArr addObject:dict];
        }
        [self createVC];
        
    }];
}


/**
 点击导航栏中间
 */
- (void)clickRank:(id)sender {
    NSLog(@"主页的导航栏中间被点击");
    [self.navigationController pushViewController:[[HomeRankViewController alloc] init] animated:YES];
}

/**
 导航左按钮
 */
- (void)navLeftAction:(id)sender {
    NSLog(@"直播页面的导航左按钮被点击");
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    searchVC.type = 2;
    [self.navigationController pushViewController:searchVC animated:YES];
}
/**
 导航右按钮
 */
- (void)navRithAction:(id)sender {
    NSLog(@"直播页面的导航右按钮被点击");
    
    NSLog(@"点击站内信");
    if (GetUserDefaule(USERINFO) == NULL) {
        [self presentViewController:[LZCTools alertMessage:@"请先登录"]animated:YES completion:nil];
    }
    else {
        [self.navigationController pushViewController:[[MessageViewController alloc] init] animated:YES];
    }
    
}

// 懒加载
- (NSMutableArray *)tagArr {
    if (_tagArr == nil) {
        _tagArr = [[NSMutableArray alloc] init];
    }
    return _tagArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
